--[[
    Author          ZhengFeng
    Time            2020.12.05
    Description     宠物融合界面-查看天赋数值列表
]]--

require "Common/define"
require "Logic/PetManager"
--local Page_PetMergeLevelUp = UIBase.New(PanelResNames.Page_PetMergeLevelUp)
local PetMergeLevelUp = Class("Page_PetMergeLevelUp")

local this = PetMergeLevelUp
local mergeListIndex = 0
local mergeList = {}
local petListIndex = 0
local MAX_MERGE_LENGTH = 5
local petListBtn = {}
--function this:Init()
--    UIBase.Init(this)
--end
function this:ctor()

end
function this:Init(obj)
    obj:GetChild("Bg1").text = getLanguage("PetMerge_Btn_Merge")
    obj:GetChild("Title2").text = getLanguage("PetMerge_PetSelect")
    this.Btn_BGClose = obj:GetChild("Btn_BGClose")
    this.Btn_Back = obj:GetChild("Btn_Back")
    this.Btn_Confirm = obj:GetChild("Btn_Confirm")
    this.List_Merge = {}
    for i = 1, 5 do
        table.insert(this.List_Merge, obj:GetChild("slot" .. i));
    end
    --this.List_Merge.itemRenderer = this.rendererMergeListItem
    this.List_Pet = obj:GetChild("List_Pet"):GetChild("List_PetLevel");
    this.List_Pet.itemRenderer = this.rendererPetListItem
    this.Txt_MergeSelected = obj:GetChild("Txt_MergeSelected")

    this.ProgressBar_Exp = obj:GetChild("ProgressBar_Exp")
    this.ProgressBar_ForecastExp = obj:GetChild("ProgressBar_ForecastExp")
    this.Txt_CurrentLevel = obj:GetChild("Txt_CurrentLevel")
    this.Txt_LevelTitle = obj:GetChild("Txt_LevelTitle")
    --this.Txt_LevelTitle.text = getLanguage("PetMerge_Level")
    this.Txt_AddLevel = obj:GetChild("Txt_AddLevel")
    this.Txt_Exp = obj:GetChild("Txt_Exp")
    this.Next_Info = obj:GetChild("Next_Info")
    this.Btn_OneClick = obj:GetChild("Btn_OneClick");
    this.transUpLevel = obj:GetTransition("upLevelShow");
    this.transUpLevelReady = obj:GetTransition("upLevelShowReady");

    this.endEffect = obj:GetChild("Effect_Cwrh_idle2")
    this.specialEffPos = {}
    this.specialEffs = {}
    for i = 1, 5 do
        local node = obj:GetChild("upLevelEff" .. i);
        table.insert(this.specialEffPos, node);
    end
    for i = 1, 5 do
        local node = obj:GetChild("Effect_Cwrh_idle1_" .. i);
        table.insert(this.specialEffs, node);
    end

    this.specialEffectList = {}
    this.showWrappers = {};
    --for i = 1, MAX_MERGE_LENGTH, 1 do
    --    local _wapTarget = obj:GetChild("SpecialEffect_" .. i).wrapTarget
    --    table.insert(this.specialEffectList, _wapTarget)
    --end
    this:initModelTable()

end

function this:AddEvent()
    this.Btn_BGClose.onClick:Add(this.onClickClose)
    this.Btn_Back.onClick:Add(this.onClickClose)
    this.Btn_Confirm.onClick:Add(this.onClickConfirm)
    this.Btn_OneClick.onClick:Add(this.oneKeyAdd)
    --ListenEvent(Config.EventType.RefreshMergePet, this.onRefreshMergePet)
    mergeList = {}
end

function this:RemoveEvent()
    mergeList = {}
    this:removeModelTable()
    --this.List_Merge.onClickItem:Remove(this.onMergeListItemClick)
    this.List_Pet.onClickItem:Remove(this.onPetListItemClick)
    this.Btn_BGClose.onClick:Remove(this.onClickClose)
    this.Btn_Back.onClick:Remove(this.onClickClose)
    this.Btn_Confirm.onClick:Remove(this.onClickConfirm)
    this.Btn_OneClick.onClick:Remove(this.oneKeyAdd)
    --CloseEvent(Config.EventType.RefreshMergePet, this.onRefreshMergePet)
end

function this:initModelTable()
    if this.mergeListModelTable == nil then
        this.mergeListModelTable = {}
    end

    if this.petListModelTable == nil then
        this.petListModelTable = {}
    end
end

function this.onShowProgress()
    this.Txt_CurrentLevel.text = tostring(PetManager.petLv)
    local f_Exp = G.dataTable["t_elvesmerge"][tostring(PetManager.petLv)].f_Exp
    this.Txt_ExpNumber.text = string.format("%d/%d", PetManager.CurrentExp, f_Exp)
    this.ProgressBar_Exp.value = math.max(0, 100 * PetManager.CurrentExp / f_Exp)
end

function this:oneKeyAdd()
    if table.getn(mergeList) >= MAX_MERGE_LENGTH then
        UISysTips.AddMsg(getLanguage("PetMerge_MoreThanMaxLength"))
        return ;
    end
    local _mergeCount = 0
    for i = 1, this.List_Pet.numChildren do
        local _obj = this.List_Pet:GetChildAt(i - 1);
        local _index = i;
        local _petInfo = PetManager.GetPetInfoByIndex(_index)
        local _canMerge = PetManager.CheckPetCanMerge(_petInfo)
        if _canMerge then
            if table.getn(mergeList) < MAX_MERGE_LENGTH then
                local _checkInMergeList = this:checkInMergeList(_petInfo.petId)
                if _checkInMergeList == false then
                    _obj.selected = true;
                    this.onPetListItemClick(_obj)
                    _mergeCount = _mergeCount + 1
                end
                -- else
                --     --UISysTips.AddMsg(getLanguage("PetMerge_MoreThanMaxLength"))
                --     return ;
            end
        end
    end

    --牧场升级点击【一键添加】
    -- TAManager.TrackByDictionary("pet_one_key_add", { count = _mergeCount })
end

function this:RefreshMergeTalent()
    this.talents = {}
    this.TalentBuffTypeController:SetSelectedIndex(this.talentSelected - 1)
    local talentsCfg = G.dataTable["t_elvestalent"];
    for i = 1, table.getn(talentsCfg) do
        table.insert(this.talents, { state = PetManager.petLv >= i and 1 or 0, value = talentsCfg[tostring(i)] })
    end
    this.List_TalentBuff.numItems = #this.talents

    local _notTalent = ""
    if #this.talents == 0 then
        _notTalent = getLanguage("PetMerge_Txt_NotTalent") --"尚无天赋加成"
    end
    this.Txt_NotTalent.text = _notTalent
end

function this:removeModelTable()
    if this.mergeListModelTable then
        for index, _ in pairs(this.mergeListModelTable) do
            this.removeMergeListModelByIndex(index)
        end
    end

    if this.petListModelTable then
        for index, _ in pairs(this.petListModelTable) do
            this.removePetListModelByIndex(index)
        end
    end
end
function this.removeMergeListModelByIndex(index)
    if this.mergeListModelTable[index].modelGO then
        destroyImmediate(this.mergeListModelTable[index].modelGO)
    end
    this.mergeListModelTable[index].modelGO = nil
    this.mergeListModelTable[index].parentGO = nil
    this.mergeListModelTable[index].petId = nil
end
function this.removePetListModelByIndex(index)
    if this.petListModelTable[index].modelGO then
        destroyImmediate(this.petListModelTable[index].modelGO)
    end
    this.petListModelTable[index].modelGO = nil
    this.petListModelTable[index].parentGO = nil
    this.petListModelTable[index].petId = nil
end

function this:onClickClose()
    this:removeModelTable()
    closeUI(PanelResNames.Page_PetMergeLevelUp)
end
function this:onClickConfirm()
    local _mergePetIdList = {}
    for _, v in ipairs(mergeList) do
        table.insert(_mergePetIdList, v.petId)
    end
    if #_mergePetIdList > 0 then
        --播放升级动画
        -- local _levelUpDic = {}
        -- _levelUpDic.current_level = PetManager.petLv
        -- _levelUpDic.current_exp = PetManager.CurrentExp
        -- TAManager.TrackByDictionary("pet_click_level_up", _levelUpDic)
        this.onRefreshMergePet(function()
            PetHandler.ReqMergePet(_mergePetIdList)
        end)

        AudioManager.Play(2025)
    end
end
function this.onRefreshMergePet(cb)
    this.transUpLevelReady:Play();
    local _mergeCount = 0;
    FireEvent(Config.EventType.RefreshOthersApplyList, true);
    --specialEffs
    for i, v in pairs(mergeList) do
        local petIndex = i
        _mergeCount = _mergeCount + 1;
        if this.mergeListModelTable then
            local tempIndex = 1
            for index, _ in pairs(this.mergeListModelTable) do
                if tempIndex == i then
                    petIndex = index
                    break ;
                else
                    tempIndex = tempIndex + 1
                end
            end
        end
        local effectShow = this.specialEffs[petIndex].spineAnimation.state;
        effectShow:SetAnimation(0, "idle1", false);
        local idleAniTime = effectShow.Data.SkeletonData:FindAnimation("idle1").Duration;
        local frameTime = idleAniTime / 40;
        --宠物消失
        if this.uTimer == nil then
            this.uTimer = {};
        end
        this.uTimer[i] = utimer.timeCall(function()
            --effect.visible = false;
            mergeList = {}
            if this.mergeListModelTable then
                for index, _ in pairs(this.mergeListModelTable) do
                    this.removeMergeListModelByIndex(index)
                end
            end
            this:showList()
            utimer.remove(this.uTimer[i]);
            this.uTimer[i] = nil;

        end, frameTime * 5 * 1000)
        --粒子出现
        if this.uTimerLizi == nil then
            this.uTimerLizi = {};
        end

        this.uTimerLizi[i] = utimer.timeCall(function()
            resMgr:LoadModelPrefab("UIEff/UI_efc_cwsj", function(go)
                _mergeCount = _mergeCount - 1;
                obj = this.specialEffectList[i];
                if (obj ~= nil) then
                    destroyImmediate(obj);
                end
                local wrapper = this.showWrappers[i];
                if wrapper == nil then
                    wrapper = GoWrapper.New()
                    this.showWrappers[i] = wrapper;
                end
                obj = newObject(go);
                obj:AddComponent(TCanvas);
                obj:AddComponent(TChangeSortingOrder)
                wrapper:SetWrapTarget(obj, false)
                Util.Lua_SetLocalPositionXYZ(obj.transform, 0, 0, 0)
                this.specialEffectList[i] = obj;
                this.specialEffPos[petIndex]:SetNativeObject(wrapper);
                if _mergeCount == 0 then
                    this.transUpLevel:Play(function()
                        this.endEffect.visible = true;
                        local effectEnd = this.endEffect.spineAnimation.state;
                        effectEnd:SetAnimation(0, "idle2", false);
                        local idleAniTime = effectEnd.Data.SkeletonData:FindAnimation("idle2").Duration;
                        this.timerEffectEnd = utimer.timeCall(function()
                            if cb then
                                cb()
                            end
                            for k, v in pairs(this.specialEffectList) do
                                local obj = this.specialEffectList[k];
                                if obj ~= nil then
                                    destroyImmediate(obj);
                                end
                                this.specialEffectList[k] = nil;
                            end
                            this.endEffect.visible = false;
                            utimer.remove(this.timerEffectEnd);
                            this.timerEffectEnd = nil;
                            FireEvent(Config.EventType.RefreshOthersApplyList, false);

                        end, idleAniTime * 1000)
                    end)
                end
            end)
            utimer.remove(this.uTimerLizi[i]);
            this.uTimerLizi[i] = nil;
        end, frameTime * 10 * 1000)
    end


end

function this:updateMergeList(idx)
    petListIndex = 0
    this:showProgress()
    --this.List_Pet.numItems = #PetManager.PetInfoList
    --this.List_Pet.onClickItem:Add(this.onPetListItemClick)

    this.removeMergeListModelByIndex(idx)

    local _touchable = false
    if table.getn(mergeList) > 0 then
        _touchable = true
    end
    this.Btn_Confirm.touchable = _touchable
    this.Btn_Confirm.enabled = _touchable
    --this.Txt_MergeSelected.text = tostring(table.getn(mergeList)) .. "/".. tostring(MAX_MERGE_LENGTH)
    this.Txt_MergeSelected.text = tostring(#PetManager.PetInfoList) .. "/" .. tostring(PetManager.MaxPetCount)
end

function this:showList(isRefresh)

    --this:removeModelTable()
    petListIndex = 0
    this:showProgress()
    if isRefresh then
        this.List_Pet.numItems = #PetManager.PetInfoList
        this.List_Pet.onClickItem:Add(this.onPetListItemClick)
    end

    mergeListIndex = 0
    for i = 1, #this.List_Merge do
        local item = this.List_Merge[i];
        this:rendererMergeListItem(item);
        item.onClick:Clear();
        item.onClick:Add(function()
            this.onMergeListItemClick(i)
        end)
    end
    --this.List_Merge.numItems = table.getn(mergeList)


    local _touchable = false
    if table.getn(mergeList) > 0 then
        _touchable = true
    end
    this.Btn_Confirm.touchable = _touchable
    this.Btn_Confirm.enabled = _touchable
    --this.Txt_MergeSelected.text = tostring(table.getn(mergeList)) .. "/".. tostring(MAX_MERGE_LENGTH)
    this.Txt_MergeSelected.text = tostring(#PetManager.PetInfoList) .. "/" .. tostring(PetManager.MaxPetCount)
end
function this:showProgress()
    local _addLevel = ""
    this._addLevel = 0;
    local _exp = ""
    local _progress = 0
    local _currentProgress = 0
    local _mergeConfig = G.dataTable["t_elvesmerge"]
    local _talentsConfig = G.dataTable["t_elvestalent"]
    local nextCfg = _mergeConfig[tostring(PetManager.petLv + 1)];
    local f_Exp = 99999999999999999;
    if nextCfg then
        f_Exp = tonumber(nextCfg["f_Exp"])
    end
    local _newExp = PetManager.CurrentExp + table.getn(mergeList)
    local _nextLv = PetManager.petLv + 1;
    local nextCfg2 = _mergeConfig[tostring(PetManager.petLv + 1)];
    local f_next2Exp = 99999999999999999;
    if nextCfg2 then
        f_next2Exp = nextCfg2["f_Exp"];
    end
    if _nextLv > PetManager.MaxPetLevel then
        _progress = 100
        _exp = "Max";
    else
        if _newExp < f_Exp then
            _addLevel = ""
            this._addLevel = 0;
            if PetManager.petLv == PetManager.MaxPetLevel then
                _exp = ""
                _progress = 100
            else
                _exp = tostring(_newExp) .. "/" .. tostring(f_Exp)
                _progress = 100 * _newExp / tonumber(tostring(f_Exp))
            end
            _currentProgress = 100 * PetManager.CurrentExp / tonumber(tostring(f_Exp))
        else
            if PetManager.petLv == PetManager.MaxPetLevel then
                _addLevel = "+1"
                this._addLevel = 1;
                _exp = tostring(f_Exp) .. "/" .. tostring(f_Exp)
                _progress = 100
            else
                local _remainingExp = _newExp - tonumber(tostring(f_Exp))
                local f_nextExp = _mergeConfig[tostring(_nextLv + 1)]["f_Exp"]
                --if PetManager.petLv == 0 and  _remainingExp == 0 then
                --    _addLevel = "";
                --    _progress = 0;
                --    _exp = tostring(_remainingExp) .. "/" .. tostring(f_nextExp)
                --else
                if _remainingExp < f_nextExp then
                    _addLevel = "+1"
                    this._addLevel = 1;
                    _exp = tostring(_remainingExp) .. "/" .. tostring(f_nextExp)
                    _progress = 100 * _remainingExp / f_nextExp
                elseif _remainingExp < f_next2Exp then
                    _addLevel = "+2"
                    this._addLevel = 2;
                    _remainingExp = _remainingExp - f_nextExp
                    _exp = tostring(_remainingExp) .. "/" .. tostring(f_next2Exp)
                    _progress = 100 * _remainingExp / f_next2Exp
                else
                    _addLevel = "+3"
                    this._addLevel = 3;
                    _remainingExp = _remainingExp - f_next2Exp
                    local f_next3Exp = _mergeConfig[tostring(PetManager.petLv + 4)]["f_Exp"]
                    _exp = tostring(_remainingExp) .. "/" .. tostring(f_next3Exp)
                    _progress = 100 * _remainingExp / f_next3Exp
                end
            end
        end
    end

    this.Txt_CurrentLevel.text = tostring(PetManager.petLv) + this._addLevel;

    this.Txt_AddLevel.text = _addLevel
    this.Txt_Exp.text = _exp
    this.ProgressBar_Exp.value = _currentProgress
    this.ProgressBar_ForecastExp.value = _progress
    if _nextLv > PetManager.MaxPetLevel then
        _nextLv = PetManager.MaxPetLevel;
    end
    local nextLvCfg = _talentsConfig[tostring(_nextLv)];
    this.Next_Info:GetChild("Text_BuffTips").text = getLanguage(nextLvCfg.f_TalentDesc);
    this.Next_Info:GetChild("Text_BuffLevel").text = nextLvCfg.f_TalentId;
    this.Next_Info:GetChild("Text_BuffNum").text = ""
end

function this:rendererMergeListItem(obj)
    mergeListIndex = mergeListIndex + 1
    local _petInfo = mergeList[mergeListIndex]
    if _petInfo == nil then
        return ;
    end
    --obj:GetChild("icon").url = _petInfo.petIcon
    --obj.selected = false
    local _petMutationLevel = PetManager.GetMutationLevel(_petInfo.genes)
    --obj:GetController("level"):SetSelectedIndex(_petMutationLevel - 1)
    --obj:GetChild("frame").url = PetManager.GetMutationLevelFrameIcon(_petMutationLevel)
    --显示宠物模型
    if this.mergeListModelTable[mergeListIndex] == nil then
        this.mergeListModelTable[mergeListIndex] = {}
    end
    local _parentGO = obj:GetChild("Model_Pet")
    if this.mergeListModelTable[mergeListIndex].modelGO
            and this.mergeListModelTable[mergeListIndex].petId == _petInfo.petId
            and this.mergeListModelTable[mergeListIndex].parentGO == _parentGO then
        --PetManager.PetModelRefresh(this.mergeListModelTable[mergeListIndex], _petInfo.genes, false)
    else
        this.removeMergeListModelByIndex(mergeListIndex)
        resMgr:LoadModelPrefab(PetManager.PetModelPrefab, function(go)
            this.mergeListModelTable[mergeListIndex].modelGO = newObject(go)

            local com = this.mergeListModelTable[mergeListIndex].modelGO:GetComponent("SkeletonGraphic")

            if this.mergeListModelTable[mergeListIndex].modelGO and com then
                com.material = resMgr.Pet_Copy_Mat
                com.material:SetFloat("_StencilComp", 8)
                com.material:SetFloat("_Stencil", 1);
            end

            this.mergeListModelTable[mergeListIndex].parentGO = _parentGO
            local _modelWrapper = this.mergeListModelTable[mergeListIndex].modelWrapper
            this.mergeListModelTable[mergeListIndex].modelWrapper = GoWrapper.New()
            this.mergeListModelTable[mergeListIndex].petId = _petInfo.petId
            PetManager.PetModelFirstSet(this.mergeListModelTable[mergeListIndex], _petInfo.genes, false, Vector3.New(0, 0, 0), Vector3.New(1, 1, 1))
            if _modelWrapper then
                _modelWrapper:Dispose()
            end
        end)
    end
end
function this.onMergeListItemClick(idx)
    local petIndex = 1
    for i, _ in pairs(this.mergeListModelTable) do
        if idx == i then
            break
        end
        if _.modelGO ~= nil then
            petIndex = petIndex + 1
        end
    end
    if  mergeList[idx] == nil then
        return;
    end
    local petId = mergeList[idx].petId;
    petListBtn[petId].selected = false;
    mergeList[idx] = nil;
    this:updateMergeList(idx)
end

function this:rendererPetListItem(obj)
    petListIndex = petListIndex + 1
    local _petInfo = PetManager.GetPetInfoByIndex(petListIndex)
    petListBtn[_petInfo.petId] = obj;
    --obj:GetChild("icon").url = _petInfo.petIcon
    -- 只有在Normal状态下才能选择
    local _controller = obj:GetController("type")
    local _selectedIndex = PetManager.GetPetItemType(_petInfo)
    _controller:SetSelectedIndex(_selectedIndex)
    obj:GetController("Lock"):SetSelectedIndex(_petInfo.IsLocked and 0 or 1)
    obj:GetController("follow"):SetSelectedIndex(_petInfo.IsFollowStatus and 0 or 1)
    local _canMerge = PetManager.CheckPetCanMerge(_petInfo)
    local _index = _canMerge and 0 or 1
    obj:GetController("cantSelect"):SetSelectedIndex(_index)

    --当前item是否被选中
    obj.selected = this:checkInMergeList(_petInfo.petId)
    local _petMutationLevel = PetManager.GetMutationLevel(_petInfo.genes)
    obj:GetController("level"):SetSelectedIndex(_petMutationLevel - 1)
    obj:GetChild("frame").url = PetManager.GetMutationLevelFrameIcon(_petMutationLevel)

    --显示宠物模型
    if this.petListModelTable[petListIndex] == nil then
        this.petListModelTable[petListIndex] = {}
    end
    if this.petListModelTable[petListIndex].modelGO and this.petListModelTable[petListIndex].petId == _petInfo.petId then
        PetManager.PetModelRefresh(this.petListModelTable[petListIndex], _petInfo.genes, false)
    else
        this.removePetListModelByIndex(petListIndex)
        resMgr:LoadModelPrefab(PetManager.PetModelPrefab, function(go)
            local n_go = newObject(go)

            this.petListModelTable[petListIndex].modelGO = n_go
            this.petListModelTable[petListIndex].parentGO = obj:GetChild("Model_Pet")
            local _modelWrapper = this.petListModelTable[petListIndex].modelWrapper
            this.petListModelTable[petListIndex].modelWrapper = GoWrapper.New()
            this.petListModelTable[petListIndex].petId = _petInfo.petId
            PetManager.PetModelFirstSet(this.petListModelTable[petListIndex], _petInfo.genes, false, Vector3.New(0, 0, 0), Vector3.New(0.55, 0.55, 1), 3)
            if _modelWrapper then
                _modelWrapper:Dispose()
            end
        end)
    end
end

function this:clearMergeList()
    mergeList = {};
    if this.mergeListModelTable then
        for index, _ in pairs(this.mergeListModelTable) do
            this.removeMergeListModelByIndex(index)
        end
    end
end

function this.onPetListItemClick(context)
    local _obj = context.data and context.data or context;
    local _index = this.List_Pet:GetChildIndex(_obj) + 1
    local _petInfo = PetManager.GetPetInfoByIndex(_index)

    local _canMerge = PetManager.CheckPetCanMerge(_petInfo)
    if _canMerge then
        if _obj.selected then
            if table.getn(mergeList) < MAX_MERGE_LENGTH then
                if (tonumber(this._addLevel) + PetManager.petLv) < PetManager.MaxPetLevel then

                    local _checkInMergeList = this:checkInMergeList(_petInfo.petId)
                    if _checkInMergeList == false then
                        this:addInMergeList(_petInfo)
                        this:showList()
                    end
                else
                    _obj.selected = false
                    UISysTips.AddMsg(getLanguage("PetMax01"))
                end
            else
                _obj.selected = false
                UISysTips.AddMsg(getLanguage("PetMerge_MoreThanMaxLength"))
            end
        else
            local idx = this:delInMergeList(_petInfo)
            --this:showList()
            if idx ~= 0 then
                local petIndex = idx
                local tempIndex = 1
                --for i, _ in ipairs(this.mergeListModelTable) do
                --    if _.modelGO ~= nil then
                --        if tempIndex == idx then
                --            petIndex = i
                --            break
                --        else
                --            tempIndex = tempIndex + 1
                --        end
                --    end
                --end
                this:updateMergeList(petIndex)
            end
        end
    else
        _obj.selected = false
        if _petInfo.IsFollowStatus then
            UISysTips.AddMsg(getLanguage("PetMerge_CannotMerge"))
        elseif _petInfo.IsLocked then
            UISysTips.AddMsg(getLanguage("PetApply_Locking"))
        else
            UISysTips.AddMsg(getLanguage("PetApply_QuasiVariational"))

        end
    end

    _obj:GetChild("icon").url = _petInfo.petIcon

end
function this:checkInMergeList(petId)
    for k, v in ipairs(mergeList) do
        if v.petId == petId then
            return true
        end
    end
    return false
end

function this:addInMergeList(petInfo)
    for i = 1, MAX_MERGE_LENGTH do
        local pet = mergeList[i]
        if pet == nil then
            mergeList[i] = petInfo
            break ;
        end
    end
end

function this:delInMergeList(petInfo)
    for i = 1, MAX_MERGE_LENGTH do
        local pet = mergeList[i]
        if pet then
            if tostring(pet.petId) == tostring(petInfo.petId) then
                mergeList[i] = nil;
                return i;
            end
        end
    end
end

return this